package com.rjet.access_control.socket;

import cn.hutool.core.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.DatatypeConverter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @Author:ethan
 * @Date: 2023/10/13  9:43
 * @Version 1.0.1
 */
@Slf4j
public class UdpServer {

    //服务器Udp端口
    public static final int SERVER_TCP_PORT  = 2402;

    //单片机网口模块ip
    public static final String MCU_USR232_IP = "192.168.5.171";

    //单片机网口模块本地短裤
    public static final int MCU_USR232_PORT  = 20108; //11201 8082

    public void udp() {

        try {

            DatagramSocket socket = new DatagramSocket(SERVER_TCP_PORT); //创建套接字

            while (true) {

                //接收服务器反馈数据
                byte[] backbuf = new byte[20];
                DatagramPacket backPacket = new DatagramPacket(backbuf, backbuf.length);
                socket.receive(backPacket); //接收返回数据
                if (backPacket.getLength() == 0) {
                    continue;
                }
                String backMsg = HexUtil.bytesToHexString(backbuf);
                String hex = HexUtil.bytesToHexString(backbuf);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = sdf.format(new Date(System.currentTimeMillis()));
                log.info("time = " + dateString + " backMsg =" + backMsg  + " len = " +
                        backMsg.length() + " hex = " + hex);

                //演示 33D4BE1B 为正确卡号 3351DD1B等其他都为错误卡号

                Thread.sleep(2000);

                //格式： 11开头 第2为代表有效数据长度 第3为代表是否合法： 01合法 其他不合法或者另外自定义 FF验证失败且不给语音提示
                byte[] buf;
                if (backMsg.toUpperCase().contains("33D4BE1B")){ //验证成功
                    buf = HexUtil.hexStringToBytes("110ACC33D4BE1B000000");
                }else if (backMsg.toUpperCase().contains("83A9E824")){ //不在刷卡时间段
                    buf = HexUtil.hexStringToBytes("110ADD33D4BE1B000000");
                }else if (backMsg.toUpperCase().contains("9D8809D9")){ //请勿重刷
                    buf = HexUtil.hexStringToBytes("110AEE33D4BE1B000000");
                }else{
                    buf = HexUtil.hexStringToBytes("110A0033D4BE1B000000"); //验证失败
                }


                InetAddress address = InetAddress.getByName(MCU_USR232_IP); //服务器地址
                DatagramPacket dataGramPacket = new DatagramPacket(buf, buf.length, address, MCU_USR232_PORT);
                socket.send(dataGramPacket);
            }

        } catch (Exception e) {
            log.info(" receive 数据异常 " + e.getLocalizedMessage());
        }

    }

    class LocationClass extends Thread {

    }


    public static void main(String[] args) {
        String str = "我是一个字符串";

        String hex = cn.hutool.core.util.HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);

//hex是：
//e68891e698afe4b880e4b8aae5ad97e7aca6e4b8b2

        String decodedStr = cn.hutool.core.util.HexUtil.decodeHexStr(hex);

//解码后与str相同
        System.out.println(hex);
        System.out.println(decodedStr);
    }

}